<!--
  ~ Copyright 2016 Red Hat, Inc. and/or its affiliates
  ~ and other contributors as indicated by the @author tags.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~ http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<html>
<head>
    <script src="js/keycloak.js"></script>
    <script>
        var keycloak = new Keycloak({
            url: '${js-adapter.auth-server-url}',
            realm: 'test',
            clientId: 'js-console'
        });
        keycloak.init({}).then(function(authenticated) {
            output('Init Success (' + (authenticated ? 'Authenticated' : 'Not Authenticated') + ')');
        }).catch(function() {
            output('Init Error');
        });

        keycloak.onAuthSuccess = function () {event('Auth Success')};
        keycloak.onAuthError = function () {event('Auth Error')};
        keycloak.onAuthRefreshSuccess = function () {event('Auth Refresh Success')};
        keycloak.onAuthRefreshError = function () {event('Auth Refresh Error')};
        keycloak.onAuthLogout = function () {event('Auth Logout')};
        keycloak.onTokenExpired = function () {event('Access token expired.')};
        keycloak.onActionUpdate = function (status) {event('AIA status: ' + status)};
    </script>
</head>
<body>

<h2>Result</h2>
<pre style="background-color: #ddd; border: 1px solid #ccc; padding: 10px;" id="output"></pre>

<h2>Events</h2>
<pre style="background-color: #ddd; border: 1px solid #ccc; padding: 10px;" id="events"></pre>


<script>
    function output(data) {
        if (typeof data === 'object') {
            data = JSON.stringify(data, null, '  ');
        }
        document.getElementById('output').innerHTML = data;
    }

    function event(event) {
        var e = document.getElementById('events').innerHTML;
        document.getElementById('events').innerHTML = new Date().toLocaleString() + "\t" + event + "\n" + e;
    }
</script>
</body>
</html>
